<template>

  <div :style="background" class="bgBackground"></div>
  <router-view></router-view>
  <el-row type="flex" justify="center">
    <el-col :span="10">
    </el-col>
  </el-row>
  <el-row type="flex" justify="center">
    <el-card shadow="always">
      <h1 class="hh1">MBTI人格测试</h1>

      <el-divider></el-divider>

      <el-form
        label-position="top"
        :model="nameValidateForm"
        ref="nameValidateForm"
        label-width="120px"
      >

        <el-form-item
          label="用户名"
          prop="name"
          class="item"
          :rules="[{ required: true, message: '用户名不能为空' }]"
        >
          <el-input
            type="text"
            v-model="nameValidateForm.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
          class="item"
          :rules="[{ required: true, message: '密码不能为空' }]"
        >
          <el-input
          
            v-model="nameValidateForm.password"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item >
          <el-button type="primary" @click="makeweb('nameValidateForm')" round
            >注册</el-button
          >
          <el-button
            type="primary"
            @click="submitForm('nameValidateForm')"
            round
            >登录</el-button
          >
          <el-button type="primary" @click="resetForm('nameValidateForm')" round
            >清空</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!-- </el-col> -->
  </el-row>
  <!-- </div> -->
</template>

<script>
// import imgUrl from "../assets/1.jpeg";
export default {
  name: "Login",
  data() {
    return {
        nameValidateForm: {
        code: "",
        name: "",
        password: "",
      },
    };
  },
  background: {
    // 背景图片地址
    backgroundImage: "url(" + require("../assets/1.jpeg") + ")",
    // 背景图片是否重复
    backgroundRepeat: "no-repeat",
    // 背景图片大小
    backgroundSize: "cover",
    // 背景颜色
    backgroundColor: "#000",
    // 背景图片位置
    backgroundPosition: "center top",
  },
  methods: {
    get_account(formName) {
      this.$axios
        .get("/login", {
          params: {
            username: this.nameValidateForm.name,
            userpassword: this.nameValidateForm.password,
          },
        })
        .then(function (res) {
          console.log(res);
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    submitForm(formName) {
      this.get_account();
      this.$refs.nameValidateForm.validate((valid) => {
        if (valid) {
          this.$axios.get("/loginsuccess").then((res) => {
            this.nameValidateForm.code = res["data"]["code"];
            if (this.nameValidateForm.code === "20") {
              this.$router.push({ path: "/index", name: "Index" });
              alert("管理员登录");
            } else if (this.nameValidateForm.code === "10") {
              this.$router.push({ path: "/sindex", name: "Sindex" });
            } else {
              alert("用户名或密码错误");
              return false;
            }
          });
        } else {
          alert("未填写完整！");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs.nameValidateForm.resetFields();
    },
    makeweb() {
      this.$router.push({ path: "/register", name: "Register" });
    },
  },
};
</script>

<style>
.content {
  margin: 0 auto;
}
.el-card {
  width: 500px;
  height: 400px;
  border-radius: 15px;
  background: rgba(0, 0, 0, 0.45);
}
.el-row {
  margin-bottom: 150px;
}
.hh1 {
  color: #FDF5E6;
  text-align: center;
}
body {
  height: 100%;
}
.bgBackground {
  background: url("../assets/Wallpaper.jpg");
  width: 140%;
  height: 160%;
  position: fixed;
  background-size: 100% 100%;
}
.item .el-form-item__label{
    color: #FFF8DC;
  }
</style>
